﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class TextMark : IExternalCommand
	{
		public static DetailCurve DrawRangLine(Autodesk.Revit.DB.Document doc, XYZ originPoint, double radius)
		{
			Autodesk.Revit.DB.View activeView = doc.ActiveView;
			double startAngle = 0.0;
			double endAngle = Math.PI*2;
			Curve curve = doc.CreatYJKArc(originPoint, radius, startAngle, endAngle, new XYZ(1.0, 1.0, 0.0).Normalize(), new XYZ(-1.0, 1.0, 0.0).Normalize());
			return doc.Create.NewDetailCurve(activeView, curve);
		}

		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			if (commandData == null)
			{
				throw new NotImplementedException();
			}
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			Selection selection = activeUIDocument.Selection;
			transaction = new Transaction(document, "文字标注");
			transaction.Start();
			Autodesk.Revit.DB.View activeView = document.ActiveView;
			if ((int)activeView.ViewType != 4 || ((int)activeView.ViewType == 4 && !(activeView as View3D).IsLocked))
			{
				YJKMessageBox.Show("请在三位轴测图功能生成的3D平面下运行该命令！");
				transaction.RollBack();
				return Autodesk.Revit.UI.Result.Failed;
			}
			Form3DTextMark form3DTextMark = new Form3DTextMark();
			new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			form3DTextMark.Show();
			Result result;
			try
			{
				List<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(Autodesk.Revit.DB.View), document);
				List<Element> elementByClass2 = YJKElementFilter.GetElementByClass(typeof(View3D), document);
				if ((int)activeView.ViewType == 4)
				{
					View3D view3D = activeView as View3D;
					if (view3D.IsLocked)
					{
						ElementId id = view3D.Id;
						List<ElementId> list = new List<ElementId>();
						for (;;)
						{
							try
							{
								XYZ xyz = selection.PickPoint("请选择标注起点");
								if (form3DTextMark.Visible)
								{
									XYZ xyz2 = new XYZ(0.0, 1.0, 0.0).Normalize() * (double.Parse(form3DTextMark.hyTextBox45Length.Text) / 304.8);
									XYZ xyz3 = new XYZ(-1.0, 1.0, 0.0).Normalize() * (double.Parse(form3DTextMark.hyTextBox90Length.Text) / 304.8);
									XYZ xyz4 = xyz + xyz2;
									XYZ xyz5 = xyz4 + xyz3;
									TextNote textNode = document.GetTextNode(document.ActiveView, xyz5, new XYZ(1.0, 1.0, 0.0), new XYZ(1.0, 1.0, 0.0), 0.00984251968503937, (Autodesk.Revit.DB.TextAlignFlags)128, (Autodesk.Revit.DB.TextNoteLeaderTypes)0, (Autodesk.Revit.DB.TextNoteLeaderStyles)1, xyz, xyz4, form3DTextMark.comboBoxText.Text.Trim().ToString());
									int length = form3DTextMark.comboBoxText.Text.Trim().Length;
									Curve curve = RevitDocumentExtension.HYCreateEllipse(xyz5, 0.85301837270341208 * (double)length, (double)length * 1.4435695538057742, new XYZ(1.0, 1.0, 0.0).Normalize(), new XYZ(-1.0, 1.0, 0.0).Normalize(), 0.0, 100.0);
									ModelCurve modelCurve = document.Create.NewModelCurve(curve, document.ActiveView.SketchPlane);
									ElementTransformUtils.MoveElement(document, modelCurve.Id, new XYZ(-1.0, 1.0, 0.0) * -0.65616797900262469);
									new List<ElementId>().Add(modelCurve.Id);
									list.Add(textNode.Id);
									list.Add(modelCurve.Id);
									continue;
								}
							}
							catch (Autodesk.Revit.Exceptions.OperationCanceledException)
							{
							}
							break;
						}
						if (list.Count != 0)
						{
							foreach (Element element in elementByClass)
							{
								Autodesk.Revit.DB.View view = (Autodesk.Revit.DB.View)element;
								if (view.Id != id)
								{
									view.HideElements(list);
								}
							}
							foreach (Element element2 in elementByClass2)
							{
								Autodesk.Revit.DB.View view2 = (Autodesk.Revit.DB.View)element2;
								if (view2.Id != id)
								{
									view2.HideElements(list);
								}
							}
						}
					}
				}
				form3DTextMark.Close();
				document.Regenerate();
				transaction.Commit();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				form3DTextMark.Close();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				form3DTextMark.Close();
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null && 3 != (int)transaction.GetStatus())
				{
					form3DTextMark.Close();
					transaction.RollBack();
				}
			}
			return result;
		}
	}
}
